<?php
/*
 * Created on Feb 7, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 	$title = 'Quảng cáo';
 	include('../includes/session.php');
	if($session->logged_in){
		if($session->userlevel == 1){
			header("Location: ../index.php");
		}
	}else{
		header("Location: login.php");
	}
	include('../includes/encrypturl.php');
	include('../models/info_model.php');
	$session->setHeader(5);	
 	include('../includes/header.inc');
	
	if (isset($_GET['SelectedBanner'])){
		$SelectedBanner =strtoupper($_GET['SelectedBanner']);
	} elseif(isset($_POST['SelectedBanner'])){
		$SelectedBanner =strtoupper($_POST['SelectedBanner']);
	}
	
	echo '<div id="wrapper">';
	echo '<div id="content">';
	echo '<div id="box">';
	
	if (isset($_GET['delete']) && $SelectedBanner) {
		if($infoModel->deleteBanner($SelectedBanner) == 0){
			echo prnMsg(_('Ảnh quảng cáo < '.$SelectedBanner.' > đã xóa.'),'success');
		}else{
			echo prnMsg(_('Ảnh quảng cáo < '.$SelectedBanner.' > chưa xóa.'),'success');
		}				
		unset ($SelectedBanner);
		unset($delete);
	}
	if (isset($SelectedBanner)) {
		echo "<div><a href='" . $_SERVER['PHP_SELF'] . '?' . SID . "'><font face='Verdana' size='2' color=red>" . _('Quay về') . "</font></a></div>";
	}
	if(!isset($SelectedBanner)){
		$result = $infoModel->getBanner();
			
		echo '<h3>Danh sách quảng cáo</h3>';
		echo '<table width="100%"><thead>';
		echo '<tr class="borderBottom">';
		echo '<th><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
		echo '<th><a href="#">Ảnh</a></th>';     
		echo '<th><a href="#">Thứ tự</a></th>';
		echo '<th><a href="#">Liên kết</a></th>';
		echo '<th><a href="#">Ngày tạo</a></th>';    
		echo '<th><a href="#">Hành động</a></th>';
		echo '</tr></thead>';
			while ($myrow = $infoModel->getArrayNameInfos($result)) {				
				echo '<tr class="borderBottom">';
					echo '<td>'. $myrow['id'] .'</td>';
					echo '<td><img src="'.$_SESSION['PathUploadBanner'].$myrow['image'].'" alt="Image Content" width="80" height="80"  /></td>';
					echo '<td>'. $myrow['sortNo'] .'</td>';
					echo '<td>'. $myrow['url'] .'</td>';
					echo '<td>'. ConvertSQLDate($myrow['createDate']) .'</td>';
					echo '<td><a href="'. $_SERVER['PHP_SELF'] . '?' . SID . '&SelectedBanner='. $myrow['id'] .'"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Cập nhật BĐS" width="16" height="16" /></a>
						<a href="'. $_SERVER['PHP_SELF'] . '?' . SID . '&SelectedBanner='. $myrow['id'] .'&delete=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Xóa BĐS" width="16" height="16" /></a></td>';
				echo '<tr/>';
			}
		echo '</table>';
	}	
	echo '</div>';
	echo '<br />';
	echo '<div id="box">';
	
	if(isset($_SESSION['success'])){
	   if($_SESSION['success']){
	   		echo prnMsg(_('Ảnh đã tạo thành công.'),'success');
	   }
	   else{
	      echo prnMsg(_('Ảnh không thể tạo'),'success');
	   }
	   unset($_SESSION['success']);
	}
	
	if(isset($_SESSION['edit'])){
   		unset($_SESSION['edit']);
   		echo prnMsg(_('Ảnh cập nhật thành công'),'success');
	}

	if($form->num_errors > 0){
	   //echo $form->num_errors." error(s) found";
	   echo $form->error("SqlErrorMessage");
	   echo $form->error("image");
	}
	
	echo "<form id='form' method='post' enctype='multipart/form-data' action='../controllers/info_controller.php" . "?" . SID . "'>";
	
	if (isset($SelectedBanner)) {
		
		$myrow = $infoModel->getBannerById($SelectedBanner);
		
		$_POST['url'] = $myrow['url'];
		$_POST['sortNo'] = $myrow['sortNo'];
		$_POST['image'] = $myrow['image'];
			
		echo "<input type=hidden name='SelectedBanner' VALUE='" . $SelectedBanner . "'>";
			
		echo '<h3 id="adduser">Cập nhật ảnh quảng cáo</h3><br/>';			
	}else{
		// khong chon select user
		echo '<h3 id="adduser">Thêm ảnh quảng cáo</h3><br/>';
	}
	if (!isset($_POST['createDate'])){
		$_POST['createDate']= $today;
	}
	
	echo '<fieldset id="personal">';
    echo '<legend>THÔNG TIN CHUNG</legend>';
	
	echo '<label for="url">Liên kết website: </label><input name="url" id="url" type="text" tabindex="1" size="70" value="'.(isset($SelectedBanner)?$_POST['url']:$form->value("url")).'"/><br />';
	echo '<label for="sortNo">Thứ tự hiển thị: </label><input name="sortNo" id="sortNo" type="text" tabindex="2" size="30" value="'.(isset($SelectedBanner)?$_POST['sortNo']:$form->value("sortNo")).'"/><br />';
	echo '<label for="image">Chọn ảnh<span class="require">(*)</span>: </label><input type="file" name="image"/><br />';
	if (!isset($SelectedBanner)) {
		$createDate = (!isset($SelectedBanner)?$_POST['createDate']:$form->value("createDate"));
		echo '<label for="createDate">Ngày tạo: </label><input name="createDate" id="createDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$createDate.'"/>';
		echo '<a href="javascript:NewCal(\'createDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';
		echo '<input type="hidden" name="banneradd" value="1" />';
	}else{
		if (($_POST['image'] != null) && file_exists($_SESSION['PathUploadBanner'].$_POST['image'])) {	
			echo '<label for="image">Ảnh: </label><img src="'.$_SESSION['PathUploadBanner'].$_POST['image'].'" alt="Image Content" /><br />';
		}else{
			echo '<label for="image">Ảnh: </label><img src="../img/NoPhotoAvailable.jpg" alt="Image Content" width="'.IMAGE_WIDTH.'" height="'.IMAGE_HEIGHT.'"  /><br />';
		}
		echo '<input type="hidden" name="banneredit" value="1" />';
	}
	echo '</fieldset>';
	echo '<div align="center">
     		<input name="submit" type="submit" value="Đồng ý" /> 
            <input name="reset" type="reset"  value="Thiết lập lại"/>
          </div></form>';
	
	echo '</div>';
	echo '</div>';echo '</div>';
	
	include('../includes/footer.inc');
?>
